﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WP09S03_OfficeTracking.DAL;
using WP09S03_OfficeTracking.Controller;
using System.Text.RegularExpressions;


namespace WP09S03_OfficeTracking
{
    public partial class FrmSuppliesIssue : WP09S03_OfficeTracking.ParentClass.ParentForm
    {
        TransactionController tran = new TransactionController();
        public FrmSuppliesIssue()
        {
            InitializeComponent();
            InitSetting();
        }

        private void InitSetting()
        {
            this.ctrlOffice.BindData_Category(1);
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string employeeID = this.txtEmployeeID.Text.Trim();
                if (employeeID == "")
                {
                    SetMessage("Invalid EmployeeID.");
                    return;
                }

                EmployeController empCtrl = new EmployeController();
                DSEmployee.EmployeeDataTable table = empCtrl.GetEmployeesByID(employeeID);

                if (table.Rows.Count == 0)
                {
                    SetMessage("Invalid EmployeeID.");
                    return;
                }
                if (this.dgvData.Rows.Count == 1)
                {
                    SetMessage("No Item Information.");
                    return;
                }
                DataTable data = new DataTable();
                data.Columns.Add("ItemID", typeof(System.String));
                data.Columns.Add("ItemDetailID", typeof(System.String));
                data.Columns.Add("Quantity", typeof(System.Int32));
                int quantity, currentQuantity;
                for (int i = 0; i < this.dgvData.RowCount - 1; i++)
                {
                    if (this.dgvData.Rows[i].Cells[4].Style.BackColor == Color.Red)
                    {
                        this.dgvData.Rows[i].Cells[4].Style.BackColor = Color.White;
                    }
                }
                for (int i = 0; i < this.dgvData.RowCount - 1; i++)
                {
                    DataRow row = data.NewRow();
                    row[0] = this.dgvData.Rows[i].Cells[0].Value.ToString();
                    row[1] = this.dgvData.Rows[i].Cells[0].Value.ToString() + "00";
                    if (!checkString(this.dgvData.Rows[i].Cells[4].Value.ToString()))
                    {
                        SetMessage("Invalid Quantity.");
                        return;
                    }

                    quantity = Convert.ToInt32(this.dgvData.Rows[i].Cells[4].Value.ToString());
                    currentQuantity = Convert.ToInt32(this.dgvData.Rows[i].Cells[5].Value.ToString());
                    if (this.dgvData.Rows[i].Cells[4].Style.BackColor.ToString() == "Red")
                    {
                        this.dgvData.Rows[i].Cells[4].Style.BackColor = Color.White;
                    }
                    if (quantity > currentQuantity)
                    {
                        SetMessage("Quantity should be less than Current Quantity. ");
                        this.dgvData.Rows[i].Cells[4].Style.BackColor = Color.Red;
                        return;
                    }
                    row[2] = quantity;
                    data.Rows.Add(row);
                }
                bool result = tran.IssueTransactionSupply(employeeID, data);
                if (result)
                {
                    clearTransactionInfo();
                    SetMessage("Transaction is successful.");
                }
                else
                {
                    SetMessage("Transaction is fail.");
                }
            }
            catch (Exception err)
            {
                SetMessage("Transaction is fail." + err.Message);
                CatchError(err);
            }
        }

        private void btnItemSelect_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ctrlOffice.dgvData.SelectedRows.Count != 0)
                {
                    for (int i = 0; i < this.ctrlOffice.dgvData.SelectedRows.Count; i++)
                    {
                        if (!Exist(this.ctrlOffice.dgvData.SelectedRows[i].Cells[0].Value.ToString()))
                        {

                            DataGridViewRow row = new DataGridViewRow();
                            row.CreateCells(this.dgvData);
                            row.Cells[0].Value = this.ctrlOffice.dgvData.SelectedRows[i].Cells[0].Value.ToString();
                            row.Cells[1].Value = this.ctrlOffice.dgvData.SelectedRows[i].Cells[3].Value.ToString();
                            row.Cells[2].Value = this.ctrlOffice.dgvData.SelectedRows[i].Cells[4].Value.ToString();
                            row.Cells[3].Value = this.ctrlOffice.dgvData.SelectedRows[i].Cells[5].Value.ToString();
                            row.Cells[4].Value = "1";
                            row.Cells[5].Value = this.ctrlOffice.dgvData.SelectedRows[i].Cells[6].Value.ToString();
                            row.Cells[6].Value = this.ctrlOffice.dgvData.SelectedRows[i].Cells[7].Value.ToString();

                            this.dgvData.Rows.Add(row);
                        }
                    }
                    isEnableSubmit();
                    SetMessage("");
                }
                else
                {
                    SetMessage("Pleaes select Item information.");
                }
            }
            catch (Exception err)
            {
                CatchError(err);
            }
        }

        private void btnEmployee_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.employeeUControl.dataGridViewEmployees.SelectedRows.Count != 0)
                {
                    this.txtEmployeeID.Text = this.employeeUControl.dataGridViewEmployees.SelectedRows[0].Cells[0].Value.ToString();
                    isEnableSubmit();
                    SetMessage("");
                    tcSearch.SelectedTab = tcSearch.TabPages[1];
                }
                else
                {
                    SetMessage("Please select employee.");
                }


                tcSearch.SelectedTab = tcSearch.TabPages[1];
            }
            catch (Exception err)
            {
                CatchError(err);
            }
        }

        private void clearTransactionInfo()
        {
            this.txtEmployeeID.Text = "";
            this.dgvData.Rows.Clear();
            this.ctrlOffice.BindData_DGV();

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dgvData.Rows.Count > 1)
                {
                    if (!this.dgvData.CurrentRow.IsNewRow)
                    {
                        this.dgvData.Rows.RemoveAt(this.dgvData.CurrentRow.Index);
                    }
                }
                isEnableSubmit();

            }
            catch (Exception err)
            {                
                CatchError(err);
            }
        }

        private bool Exist(string itemDetail)
        {
            foreach (DataGridViewRow row in this.dgvData.Rows)
            {
                if (row.Cells[0].Value != null)
                {
                    if (row.Cells[0].Value.ToString() == itemDetail)
                    {
                        row.Cells[4].Value = Convert.ToInt32(row.Cells[4].Value.ToString()) + 1;
                        return true;
                    }
                }
            }
            return false;
        }

        private void FrmSuppliesIssue_Load(object sender, EventArgs e)
        {
            btnItemSelect.Enabled = false;
            this.btnEmployee.Enabled = false;
            isEnableSubmit();
            ctrlOffice.dgvData.SelectionChanged += dgvData_ItemSelectionChanged;
            employeeUControl.dataGridViewEmployees.SelectionChanged += dgvData_EmployeeSelectionChanged;
        }
        private void dgvData_ItemSelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.ctrlOffice.dgvData.SelectedRows.Count > 0)
                {
                    btnItemSelect.Enabled = true;
                }
                else
                {
                    btnItemSelect.Enabled = false;
                }
            }
            catch (Exception err)
            {
                CatchError(err);
            }
        }

        private void dgvData_EmployeeSelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.employeeUControl.dataGridViewEmployees.SelectedRows.Count > 0)
                {
                    this.btnEmployee.Enabled = true;
                }
                else
                {
                    this.btnEmployee.Enabled = false;
                }
            }
            catch (Exception err)
            {
                CatchError(err);
            }
        }

        private bool checkString(string a)
        {
            Regex reg = new Regex("^[0-9]*[1-9][0-9]*$");
            if (reg.IsMatch(a))
                return true;
            else
                return false;
        }

        private void isEnableSubmit()
        {
            if (this.dgvData.Rows.Count > 1 && this.txtEmployeeID.Text.Trim() != "")
            {
                this.btnDelete.Enabled = true;
                this.btnSubmit.Enabled = true;
            }
            else
            {
                this.btnDelete.Enabled = false;
                this.btnSubmit.Enabled = false;
            }
        }
    }
}
